#!/usr/bin/env ruby
               
WORKDIR = '/tmp'
LOGDIR = '/tmp'
LOG_FILE = File.join(LOGDIR, 'convert_mp3.log')

  Dir.chdir( WORKDIR )
  log_file = File.open(LOG_FILE, "a+")
  [STDIN, STDOUT, STDERR].each {|desc| desc.reopen(log_file)}

  filename=ARGV[0]
  if filename.nil?
    puts "Error: Filename expected"
    exit
  end
  
  #filename can be a basename or with a .wav ext
  filename = filename.sub(/\.wav$/, '')
 
  unless File.readable?(filename + '.wav')
    #try G729 decode if not a standard .wav file
    %w{r w}.each do |d| 
      puts `g729_decode #{filename}-#{d}.G729 #{filename}-#{d}.pcm`
    end
     
    //mix
    puts `sox -m  -t raw -s -w -r 8000 #{filename}-r.pcm -t raw -s -w -r 8000 #{filename}-w.pcm #{filename}.wav`
  end

  puts `lame -h -b 32 -m m --resample 11 #{filename}.wav #{filename}.mp3`

